package cn.dnui_yxk801.dnuifood_16110100801.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.MainActivity;
import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.CollectBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.UserBean;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.UserModel;

public class LoginActivity extends BaseActivity implements OnRetrofitListener<UserBean>,View.OnClickListener{
    private UserModel model;
    private Button btn_login;
    private Button btn_reg;
    private EditText editname;
    private EditText editpass;
    public static String username;
    @Override
    void initViews() {
        layout_file=R.layout.activity_login;
        setLayout(layout_file);
        btn_login=findViewById(R.id.btn_login);
        btn_reg=findViewById(R.id.btn_reg);
        editname=findViewById(R.id.editname);
        editpass=findViewById(R.id.editpass);
    }

    @Override
    void initEvents() {
        btn_login.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
    }

    @Override
    void initDatas() {
        username=editname.getText().toString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login);

//        btn_reg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent i=new Intent(LoginActivity.this,RegisterActivity.class);
//                startActivity(i);
//            }
//        });

    }

    @Override
    public void onSuccess(UserBean object, int flag) {
        Toast.makeText(LoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
        Intent i=new Intent(LoginActivity.this, MainActivity.class);
        i.putExtra("userid",object.getUserid().toString());
        startActivity(i);
    }



    @Override
    public void onFalure(String msg) {
        Toast.makeText(LoginActivity.this, "登录失败！", Toast.LENGTH_SHORT).show();

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_login:
                String username=editname.getText().toString();
                String userpass=editpass.getText().toString();
                model=new UserModel();
                model.login(username,userpass,this);
                break;
            case R.id.btn_reg:
                Intent i=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(i);
                break;

    }}

}
